package com.ruicar.afs.cloud.batch.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.batch.entity.AfsBatchJobStepBreakPoint;
import com.ruicar.afs.cloud.batch.mapper.AfsBatchJobStepBreakPointMapper;
import com.ruicar.afs.cloud.batch.service.AfsBatchJobStepBreakPointService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.data.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class AfsBatchJobStepBreakPointServiceImpl extends ServiceImpl<AfsBatchJobStepBreakPointMapper, AfsBatchJobStepBreakPoint> implements AfsBatchJobStepBreakPointService {

    @Override
    @DS(CommonConstants._BASE_DS_NAME)
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void saveBreakPoint(String systemName,String batchNo,String stepNo, String bhDate, String breakPoint) {
        AfsBatchJobStepBreakPoint afsBatchJobStepBreakPoint = this.getOne(Wrappers.<AfsBatchJobStepBreakPoint>lambdaQuery().eq(AfsBatchJobStepBreakPoint::getBatchDate,bhDate).eq(AfsBatchJobStepBreakPoint::getSystemName,systemName).eq(AfsBatchJobStepBreakPoint::getBatchNo,batchNo).eq(AfsBatchJobStepBreakPoint::getStepNo,stepNo),false);
        if(afsBatchJobStepBreakPoint ==null){
            afsBatchJobStepBreakPoint = new AfsBatchJobStepBreakPoint();
            afsBatchJobStepBreakPoint.setBatchDate(bhDate);
            afsBatchJobStepBreakPoint.setSystemName(systemName);
            afsBatchJobStepBreakPoint.setBatchNo(batchNo);
            afsBatchJobStepBreakPoint.setStepNo(stepNo);
            afsBatchJobStepBreakPoint.setBreakPoint(breakPoint);
            this.save(afsBatchJobStepBreakPoint);
        }else {
            afsBatchJobStepBreakPoint.setBreakPoint(breakPoint);
            this.updateById(afsBatchJobStepBreakPoint);
        }
    }

    @Override
    @DS(CommonConstants._BASE_DS_NAME)
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public String loadBreakPoint(String systemName,String batchNo,String stepNo, String bhDate) {
        AfsBatchJobStepBreakPoint afsBatchJobStepBreakPoint = this.getOne(Wrappers.<AfsBatchJobStepBreakPoint>lambdaQuery().eq(AfsBatchJobStepBreakPoint::getBatchDate,bhDate).eq(AfsBatchJobStepBreakPoint::getSystemName,systemName).eq(AfsBatchJobStepBreakPoint::getBatchNo,batchNo).eq(AfsBatchJobStepBreakPoint::getStepNo,stepNo),false);
        if(afsBatchJobStepBreakPoint !=null){
            return afsBatchJobStepBreakPoint.getBreakPoint();
        }else {
            return "";
        }
    }
}
